package com.example.trafficexam2;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;

/**
 * Created by Administrator on 2018/11/26/026.
 */

public class RegularBusTimeSelectActivity extends Activity {

    private View view;
    private Button nextBtn;
    private CalendarView calendarView;
    private String selectTimeString;
    private TextView selectTimeTv;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.regular_bus_time_select_activity);
        initView();
        initdata();
    }

    private void initView(){
        nextBtn = findViewById(R.id.regular_next);
        calendarView = findViewById(R.id.calendarview);
        selectTimeTv = findViewById(R.id.select_time);
    }

    private void initdata(){

        selectTimeString = "";
        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView calendarView, int i, int i1, int i2) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                if (selectTimeString == ""){
                    selectTimeString = simpleDateFormat.format(calendarView.getDate());
                }else {
                    selectTimeString = selectTimeString+","+simpleDateFormat.format(calendarView.getDate());
                }

                selectTimeTv.setText(selectTimeString);

            }
        });

                nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sharedPreferences = getSharedPreferences("selectTime", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString("selectTime",selectTimeString);
                editor.commit();
                Toast.makeText(RegularBusTimeSelectActivity.this, "存储成功", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(RegularBusTimeSelectActivity.this,RegularBusInfoActivity.class);
                startActivity(intent);
            }
        });
    }
}
